S 1 ベクトルと行列

矢野健太郎・田代嘉宏,1991,『社会科学者のための基礎数学(改訂版)』裳華房,1-2章の問題をRで解いていく.よい子は最初は手計算しよう.

だいたいのところで,tidyverseを用いている.最初に用いる際には,以下のコードを実行してあらかじめ環境にパッケージをインストールしておく必要がある.

install.packages("tidyvese")

Rでベクトルはc()で構成されるが,行ベクトル列ベクトルの区別がないので注意.行ベクトル列ベクトルを厳密に区別したいときは,matrixを用いる.

1.1 問1.1

a <- c(2,1,4)
b <- c(5,4,-5)
c <- c(3,2,1)

a+3*b
## [1]  17  13 -11
a-b+2*c
## [1]  3  1 11
4*a+2*b-6*c
## [1] 0 0 0

1.2 問1.2

1次元ベクトルから行列を構成する場合にはmatrix関数でベクトルを入れた上で行数列数を指定する.1列目から順にベクトルの要素を埋めていく.行優先で要素を埋めたい場合は,オプションでbyrow = TRUEとする.

A <- matrix(c(2,4,1,-6,-3,5),nrow = 2, ncol = 3)
B <- matrix(c(1,3,4,3,0,-1),nrow = 2, ncol = 3, byrow = TRUE)
A+B
##      [,1] [,2] [,3]
## [1,]    3    4    1
## [2,]    7   -6    4
A-B
##      [,1] [,2] [,3]
## [1,]    1   -2   -7
## [2,]    1   -6    6
3*A
##      [,1] [,2] [,3]
## [1,]    6    3   -9
## [2,]   12  -18   15
2*A+3*B
##      [,1] [,2] [,3]
## [1,]    7   11    6
## [2,]   17  -12    7

1.3 問1.3

行列の積はRでは%*%,ただの*だと各要素事のかけ算を返すので注意.

A <- matrix(c(2,4,1,-6,-3,5),nrow = 2, ncol = 3)
B <- matrix(c(-1,3,2,3,0,4),nrow = 3, ncol = 2)
A %*% B
##      [,1] [,2]
## [1,]   -5   -6
## [2,]  -12   32

一般的に言うと,\((m,n)\)行列\(A\)\((n,l)\)行列\(B\)の積\(AB\)は,\(A\)\(i\)行目の行ベクトル\(\mathbf{a}_{i\cdot}=(a_{i1}, a_{i2}, \cdots, a_{in})\)\(B\)\(j\)列目の列ベクトル\(\mathbf{b}_{\cdot j}=(b_{1j}, b_{2j}, \cdots, b_{nj})\)の内積(inner product) \[\mathbf{a}_{i\cdot}*\mathbf{b}_{\cdot j}=\sum_{k=1}^n a_{ik}b_{kj}\]\((i,j)\)要素とする\((m,l)\)行列になる.この定義に従って行列の積関数を自作すると以下のようになる.

seki <- function(A, B) {
  if (dim(A)[2] == dim(B)[1]) {
    nrow <- dim(A)[1]
    ncol <- dim(B)[2]
    vec <- c()
    for (j in 1:ncol) {
      for (i in 1:nrow) {
        vec <- append(vec,A[i,] %*% B[,j])
      }
    }
    matrix(vec, nrow , ncol)
  } else {
    stop("かけ算できないよ!\n")
  }
}
seki(A, B)
##      [,1] [,2]
## [1,]   -5   -6
## [2,]  -12   32
A <- matrix(c(-1,3,2,3,0,4),nrow = 3, ncol = 2)
B <- matrix(c(2,4,1,-6,-3,5),nrow = 2, ncol = 3)
A %*% B
##      [,1] [,2] [,3]
## [1,]   10  -19   18
## [2,]    6    3   -9
## [3,]   20  -22   14
A <- matrix(c(3,1,-6,5,2,3,-4,1,2),nrow = 3, ncol = 3)
B <- matrix(c(1,2,3), nrow = 3, ncol = 1)
A %*% B
##      [,1]
## [1,]    1
## [2,]    8
## [3,]    6
A <- diag(3) # 単位行列E
B <- matrix(c(2,4,5,3,-1,-2), nrow = 3, ncol = 2)
A %*% B
##      [,1] [,2]
## [1,]    2    3
## [2,]    4   -1
## [3,]    5   -2

1.4 問1.4

A <- matrix(c(1,0,5,1),2,2)
r <- 10
B <- A

for (i in 1:(r-1)) {
   B <- B %*% A
}
B
##      [,1] [,2]
## [1,]    1   50
## [2,]    0    1
A <- matrix(c(0,0,0,1,0,0,3,2,0),3,3)
r <- 10
B <- A

for (i in 1:(r-1)) {
   B <- B %*% A
}
B
##      [,1] [,2] [,3]
## [1,]    0    0    0
## [2,]    0    0    0
## [3,]    0    0    0
A <- matrix(c(1,0,0,1,1,0,1,1,1),3,3)
r <- 10
B <- A

for (i in 1:(r-1)) {
   B <- B %*% A
}
B
##      [,1] [,2] [,3]
## [1,]    1   10   55
## [2,]    0    1   10
## [3,]    0    0    1

1.5 演習問題1

1

a <- matrix(c(3,-1),2,1)
b <- matrix(c(-2,0),1,2)
A <- matrix(c(1,3,2,-2),2,2)
B <- matrix(c(4,2,3,-3,1,5),2,3)
C <- matrix(c(1,2,-3,1,-1,2),3,2)

a %*% b
##      [,1] [,2]
## [1,]   -6    0
## [2,]    2    0
b %*% a
##      [,1]
## [1,]   -6
A %*% B
##      [,1] [,2] [,3]
## [1,]    8   -3   11
## [2,]    8   15   -7
C %*% B
##      [,1] [,2] [,3]
## [1,]    6    0    6
## [2,]    6    9   -3
## [3,]   -8  -15    7
A %*% B %*% C
##      [,1] [,2]
## [1,]  -31   33
## [2,]   59  -21
2*(B %*% C) + 3*A
##      [,1] [,2]
## [1,]   17   12
## [2,]  -29   24
A %*% a
##      [,1]
## [1,]    1
## [2,]   11
b %*% A
##      [,1] [,2]
## [1,]   -2   -4

2

P <- matrix(c(0.3,0.7,0.4,0.6),2,2)

P %*% P
##      [,1] [,2]
## [1,] 0.37 0.36
## [2,] 0.63 0.64
P %*% P %*% P
##       [,1]  [,2]
## [1,] 0.363 0.364
## [2,] 0.637 0.636
P %*% P %*% P %*% P
##        [,1]   [,2]
## [1,] 0.3637 0.3636
## [2,] 0.6363 0.6364

3

一例として,

A <- matrix(c(0,1,1,0),2,2)
A
##      [,1] [,2]
## [1,]    0    1
## [2,]    1    0
A %*% A
##      [,1] [,2]
## [1,]    1    0
## [2,]    0    1

一例として,

A <- matrix(c(1,-1,1,-1),2,2)
A
##      [,1] [,2]
## [1,]    1    1
## [2,]   -1   -1
A %*% A
##      [,1] [,2]
## [1,]    0    0
## [2,]    0    0